<template>
  <div class="ma-content-block">
	  <a-tabs v-model="activeTab" @change="handleTabChange">
		  
	    <a-tab-pane key="19" title="供应"></a-tab-pane>
	    <a-tab-pane key="20" title="需求"></a-tab-pane>

	  </a-tabs>
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="商品名称" field="product_name">
            <a-input v-model="searchForm.product_name" placeholder="请输入商品名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="商品副标题" field="product_subtitle">
            <a-input v-model="searchForm.product_subtitle" placeholder="请输入商品副标题" />
          </a-form-item>
        </a-col>
     
        <a-col :span="8">
          <a-form-item label="产品分类" field="product_category">
			  <a-select
			    v-model="searchForm.product_category"
			    :options="productCategoryData"
			    allow-clear
			    placeholder="请选择产品分类"
			  />
          </a-form-item>
        </a-col>
		<a-col :span="8">
		  <a-form-item label="是否推荐" field="is_recommend">
			  <sa-select v-model="searchForm.is_recommend" dict='yes_or_no' placeholder="请输入是否推荐" />
		  </a-form-item>
		</a-col>
      </template>
	  <template #is_recommend="{ record }">
	    <sa-switch v-model="record.is_recommend" checkedText="是"  uncheckedText="否" @change="changeStatus($event, record.sd_id)"></sa-switch>
	  </template>
	   <template #sort_order="{ record }">
	          <a-input-number 
	            v-model="record.sort_order"
	            :min="0"
	            style="width: 80px"
	            @change="changeSortOrder(record.sd_id, record.sort_order)"
	          />
	  </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/demand'
import commonApi from '@/api/common'
// 引用定义
const crudRef = ref()
const editRef = ref()
const activeTab = ref('zh') // 默认选中中文
const productCategoryData=ref('')
const supplyDemandData=ref('')
// 搜索表单
const searchForm = ref({
  product_name: '',
  product_subtitle: '',
  supply_demand: '',
  product_category: '',
  is_recommend:''
})


// SaTable 基础配置
const options = reactive({
 api: (params) => api.getPageList({ ...params, ...searchForm.value }),
  pk: 'sd_id',
  recycleApi:(params) => api.getRecyclePageList({ ...params, ...searchForm.value }),
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/supplyDemand/supplyDemand/save'],
    func: async () => {
      const currentLangCode = searchForm.value.lang_code;
      editRef.value?.open()
      editRef.value?.setFormData({lang_code:currentLangCode})
    },
  },
  edit: {
    show: true,
    auth: ['/supplyDemand/supplyDemand/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/supplyDemand/supplyDemand/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/supplyDemand/supplyDemand/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/supplyDemand/supplyDemand/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
   export: { show: true, url: '/supplyDemand/supplyDemand/export', auth: ['/supplyDemand/supplyDemand/export'] },
})

// SaTable 列配置
const columns = reactive([
	{
	   title: '排序序号', 
	   dataIndex: 'sort_order', 
	   width: 180,
	   slots: { customRender: 'sort_order' } // 新增插槽配置
	 },
  { title: '商品名称', dataIndex: 'product_name', width: 180 },
  { title:'商品图片',dataIndex:'image',type: 'image',width:180},
  { title: '商品副标题', dataIndex: 'product_subtitle', width: 180 },
  { title: '供应/需求', dataIndex: 'supply_demand_text', width: 120 },
  { title: '产品分类', dataIndex: 'product_category_text', width: 180 },
  { title:'推荐',dataIndex:'is_recommend',width:180}
])
const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}
const changeSortOrder = async (sd_id, sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: sd_id,
      sort_order: sortOrder
    });
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.error('修改排序出错:', error);
    Message.error('排序修改失败');
  }
}
// 页面数据初始化
const initPage = async () => {
    searchForm.value.lang_code='zh'
	 searchForm.value.supply_demand='19'
	const productCategory = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate',{classification_type:1})
	productCategoryData.value = productCategory.data
    const constsupplyDemand = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate',{classification_type:16})
	supplyDemandData.value=constsupplyDemand.data
}
const handleTabChange = async (key) => {
   activeTab.value = key
   // 设置语言代码
   searchForm.value.supply_demand = key
   // 刷新表格数据
   crudRef.value?.refresh()
}
// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
